﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using LorraineSec.DatabaseLib;

namespace LorraineSec.DatabaseLib.DataObjects
{
    public class User
    {
        #region Constants
        private const string ROOT_STR = XmlDatabase.USER;
        private const string ID_STR = XmlDatabase.ID;
        private const string ACTIVE_STR = XmlDatabase.ACTIVE;
        private const string USERNAME_STR = XmlDatabase.USERNAME;
        private const string PASSWORD_STR = XmlDatabase.PASSWORD;
        private const string FIRSTNAME_STR = XmlDatabase.FNAME;
        private const string LASTNAME_STR = XmlDatabase.LNAME;
        private const string RFID_STR = XmlDatabase.RFID;
        private const string USERGROUP_STR = XmlDatabase.USERGROUPID;
        #endregion

        private XmlNode xmlUserRoot;
        private XmlNode xmlActive;
        private XmlNode xmlUsername;
        private XmlNode xmlPassword;
        private XmlNode xmlFirstName;
        private XmlNode xmlLastName;
        private XmlNode xmlRFID;
        private XmlNode xmlUserGroup;

        public User(XmlNode node)
        {
            xmlUserRoot = node;
            xmlActive = xmlUserRoot.SelectSingleNode(ACTIVE_STR);
            xmlUsername = xmlUserRoot.SelectSingleNode(USERNAME_STR);
            xmlPassword = xmlUserRoot.SelectSingleNode(PASSWORD_STR);
            xmlFirstName = xmlUserRoot.SelectSingleNode(FIRSTNAME_STR);
            xmlLastName = xmlUserRoot.SelectSingleNode(LASTNAME_STR);
            xmlRFID = xmlUserRoot.SelectSingleNode(RFID_STR);
            xmlUserGroup = xmlUserRoot.SelectSingleNode(USERGROUP_STR);
        }

        public static List<User> GetList(XmlDatabase db)
        {
            List<User> retList = new List<User>();
            if (db.users != null)
            {
                XmlNodeList nodeList = db.users.SelectNodes(ROOT_STR);
                retList.Capacity = nodeList.Count;
                foreach (XmlNode n in nodeList)
                {
                    retList.Add(new User(n));
                }
            }
            return retList;
        }

        public static implicit operator User(XmlNode node)
        {
            return new User(node);
        }

        public static explicit operator XmlNode(User u)
        {
            return u.xmlUserRoot;
        }

        public uint ID
        {
            get { return UInt32.Parse(xmlUserRoot.Attributes[ID_STR].Value); }
        }

        public string IsActive
        {
            get { return xmlActive.InnerText; }
            set { xmlActive.InnerText = value; }
        }

        public string Username
        {
            get { return xmlUsername.InnerText; }
            set { xmlUsername.InnerText = value; }
        }

        public string Password
        {
            get { return xmlPassword.InnerText; }
            set { xmlPassword.InnerText = value; }
        }

        public string FirstName
        {
            get { return xmlFirstName.InnerText; }
            set { xmlFirstName.InnerText = value; }
        }

        public string LastName
        {
            get { return xmlLastName.InnerText; }
            set { xmlLastName.InnerText = value; }
        }

        public string RFID
        {
            get { return xmlRFID.InnerText; }
            set { xmlRFID.InnerText = value; }
        }

        public uint UserGroup
        {
            get { return UInt32.Parse(xmlUserGroup.InnerText); }
            set { xmlUserGroup.InnerText = value.ToString(); }
        }

        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }
}
